package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.ZnhjEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author zyk
 * Spring Data JPA repository for the Province entity.
 */
@Repository
public interface ZnhjRepository extends JpaRepository<ZnhjEntity, Serializable> {

    @Query(value = "select * FROM znhj_entity where unique_code= :uniqueCode and bottle_code= :bottleCode and status = 1",nativeQuery = true)
    ZnhjEntity findByUniqueCodeAndBottomCode(@Param("uniqueCode")String uniqueCode,@Param("bottleCode")String bottleCode);

    @Query(value = "select is_activity FROM znhj_entity where unique_code= :uniqueCode",nativeQuery = true)
    Integer findIsActivate(@Param("uniqueCode")String uniqueCode);

    @Query(value = "select * FROM znhj_entity where bottle_code= :bottleCode",nativeQuery = true)
    ZnhjEntity finByBottomCode(@Param("bottleCode")String bottleCode);

    @Modifying
    @Query(value = "update znhj_entity set  status = 2 where bottle_code in (:collection)",nativeQuery = true)
    void batchUpdate(@Param("collection")Collection<String> collection);
    @Modifying
    @Query(value = "update znhj_entity set  is_scan = 1 where bottle_code = :bottleCode and sku_id = :skuId",nativeQuery = true)
    void updateIsScan(@Param("bottleCode")String bottomCode, @Param("skuId")String skuId);
    @Query(value = "select * FROM znhj_entity where is_san= 1",nativeQuery = true)
    List<ZnhjEntity> findIsSanCode();

    ZnhjEntity findByBottleCode(@Param("bottleCode")String bottleCode);
    @Query(value = "select * FROM znhj_entity where status= 1 and bottle_unique in (:list)",nativeQuery = true)
    List<ZnhjEntity> findByCondition(@Param("list")List<String> list);

    @Query(value = "select * FROM znhj_entity where bottle_unique= :bottleUnique and is_activity =1",nativeQuery = true)
    ZnhjEntity finByBottomUniqueCode(@Param("bottleUnique")String bottleUnique);

    @Transactional
    @Modifying
    @Query(value = "update znhj_entity set  is_prize = 1 where bottle_unique = :bottleUnique",nativeQuery = true)
    void updateByBottomUniqueCode(@Param("bottleUnique")String bottleUnique);

}
